<?php
/*
he小题分汇总
*/
class DefaultController extends FController {
    public $user_school_list;
    public $user_subject_list;
    public $user_role_list;
    public $exam_id;
    
	//权限
	public function accessRules() {
		$config = array(
		'index' => 1,
		'update' => 1,
		'delete' => 1,
		'add' => 1
		);
		return ZRole::auth("role", $config);
	}
	
	public function __common() {
		parent::init();
		if (empty($this->cLoginUser)) {
			$this->redirect($this->createUrl('/site/login'));
        } else {
            $userLoginUser = $this->cLoginUser;
            $this->user_role_list = isset($userLoginUser['cuser']['role_id']) ? $userLoginUser['cuser']['role_id'] : '';
            $this->user_school_list = isset($userLoginUser['cuser']['school']['school_id']) ? $userLoginUser['cuser']['school']['school_id'] : '';
            $this->user_subject_list = isset($userLoginUser['cuser']['school']['subject_id']) ? $userLoginUser['cuser']['school']['subject_id'] : '';
            $zRow = new ZRole();
            //得到学校对应的考试
            $exam_list = $zRow->_getSchoolExam($this->user_school_list);
            if (!empty($exam_list)) {
                $this->exam_id = implode(',', $exam_list);
            }
		}
		$this->commonScript();
		Yii::app()->clientScript->registerCssFile(THEME_CSS_URL . '/module_Sip.css');
	}
	
	//列表展示
	public function actionIndex() {
		$this->__common();
		$this->title = "小题分汇总设置";
		
		$limit = 8;
        $wh = '';
        $date['exam_id'] = $this->request->getParam('exam_id', '');
        $date['subject_id'] = $this->request->getParam('subject_id', '');
        $where = 'WHERE 1=1 ';
        
        //权限判断
        if ($this->user_role_list != 1 && !empty($this->exam_id)) {
        	$wh = ' AND "exam_id" IN (' . $this->exam_id . ')';
        } else {
            if ($date['exam_id'] > 0) {
                $where .= " AND \"exam_id\" = " . $date['exam_id'];
            } else {
                if(!empty($this->exam_id)){
                    $where .= " AND \"exam_id\" IN (" . $this->exam_id . ")";
                }
            }
        }
                
		$date['examall'] = ZTask::_getExamAll($wh);
        
        $examdata = new ExamData();
        if ($this->user_role_list != 1) {//科目权限设定
            $date['subjectall'] = $examdata->_getSubjectList($this->user_subject_list);
            if (!empty($date['subject_id'])) {
                $where .= " AND \"subject_id\" = " . $date['subject_id'];
            } else {
                $where .= " AND \"subject_id\" IN (" . $this->user_subject_list . ")";
            }
        }else{
            $date['subjectall'] = $examdata->_getSubject();
            if (!empty($date['subject_id'])) {
                $where .= " AND \"subject_id\" = " . $date['subject_id'];
            }
        }
		
		$sql = 'SELECT * FROM "{{question_sum}}" '.$where;
		$count = 'SELECT count("id") FROM "{{question_sum}}" '.$where;
		
		$data = ZCommon::getDaoPageList($sql.' ORDER BY  "id" DESC ', $count, $limit);
		$pageUrl = $this->createUrl('/question_collect/default/index',array('exam_id'=>$date['exam_id'],'subject_id'=>$date['subject_id']));	
		
		$question_object = Nojoin::_getnojointuleconfig_where();
		$this->render('index', array(
			'data' => $data['listDatas'],
			'pages' => $data['pages'],
			'pageUrl' => $pageUrl,
			'date' => $date,
			'qdata'	=> $question_object
		));
	}
	
	//添加
	public function actionAdd() {

            $this->__common();
            $examdata = new ExamData();
            if ($this->user_role_list != 1) {//科目权限设定
                $date['subjectall'] = $examdata->_getSubjectList($this->user_subject_list);
                if (!empty($this->exam_id)) {
                    $wh = ' AND "exam_id" IN (' . $this->exam_id . ')';
                }
                $date['examall'] = ZTask::_getExamAll($wh);
            }else{
                $date['subjectall'] = $examdata->_getSubject();
		$date['examall'] = ZTask::_getExamAll();
            }
		//$date['examall'] = ZTask::_getExamAll();
		//$date['subjectall'] = ExamData::_getSubject();
		$date['row_config'] = Nojoin::_getnojointuleconfig_where();
		$date['collect_mode'] = ZTask::_getcollect_mode();
		
		if (!empty($_POST['submit'])){
			$data = $this->request->getParam('info');
            $data['no_statis_unline'] = isset($data['no_statis_unline'])?1:0;
			if (empty($data['exam_id']) && empty($data['subject_id']) && empty($data['collect_fields']) && empty($data['collect_mode'])){
				$this->runShowMessge("index",'数据不能为空');exit();
			}else {
				$questionsum = new QuestionSum();
				$data['create_user']= $this->cLoginUser['cuser']['id'];
				$data['create_time']= time();
				
				//判定当前是否已经添加
				//$sql = 'SELECT "id" FROM "{{question_sum}}" WHERE "exam_id"=:eid AND "subject_id"=:sub_id AND "collect_mode"=:cmode AND "collect_fields" = :cfile';
				$sql = 'SELECT "id" FROM "{{question_sum}}" WHERE "exam_id"=:eid AND "subject_id"=:sub_id AND "collect_fields" = :cfile';
				$result = Yii::app()->db->createcommand($sql);
				$result->bindParam(':eid',$data['exam_id'],PDO::PARAM_STR);
				$result->bindParam(':sub_id',$data['subject_id'],PDO::PARAM_STR);
				//$result->bindParam(':cmode',$data['collect_mode'],PDO::PARAM_STR);
				$result->bindParam(':cfile',$data['collect_fields'],PDO::PARAM_STR);
				$questionData = $result->queryRow();
				if(!empty($questionData)){
					$this->frameAjaxMessage(null,'数据已经存在');
					exit;		
				}
				
				if ($data['collect_mode']==1){
					$data['collect_question'] = '';
					if (empty($data['collect_questions'])){
						$this->frameAjaxMessage(null,'题型选择不能为空');exit();
					}else {
						$questionsum->attributes = $data;
						if ($questionsum->save()){
							//同步小题分数汇总
							$zExam = new zExam();
							$data = $zExam->_getRuleList($data['collect_fields'],$data['exam_id']);
							
							$this->frameAjaxMessage('/question_collect/default/index');
						}else {
							$this->frameAjaxMessage('/question_collect/default/index','数据添加失败');exit();
						}
					}
					
				}
				if ($data['collect_mode']==2){
					
					if (empty($data['collect_question'])){
						$this->frameAjaxMessage(null,'题号选择不能为空');exit();
					}else {
						
						$data['collect_question'] = implode(",",$data['collect_question']);
						$questionsum->attributes = $data;
						if ($questionsum->save()){
							
							//同步小题分数汇总
							$zExam = new zExam();
							$data = $zExam->_getRuleList($data['collect_fields'],$data['exam_id']);
							
							$this->frameAjaxMessage('/question_collect/default/index');
						}else {
							$this->frameAjaxMessage('/question_collect/default/index','数据添加失败');exit();
						}
					}
					
				}
			}
		}
		$this->render('add', array('date' => $date));exit();
	}
	
	//修改
	public function actionupdate() {
		$this->__common();
        $examdata = new ExamData();
        if ($this->user_role_list != 1) {//科目权限设定
            $date['subjectall'] = $examdata->_getSubjectList($this->user_subject_list);
            if (!empty($this->exam_id)) {
                $wh = ' AND "exam_id" IN (' . $this->exam_id . ')';
            }
            $date['examall'] = ZTask::_getExamAll($wh);
        }else{
            $date['subjectall'] = $examdata->_getSubject();
            $date['examall'] = ZTask::_getExamAll();
        }
		$date['row_config'] = Nojoin::_getnojointuleconfig_where();
		$date['collect_mode'] = ZTask::_getcollect_mode();
		$id = Yii::app()->request->getparam('id');
		$model = QuestionSum::model();
		if (!empty($_POST['submit'])){
			$data = $this->request->getParam('info');
            $data['no_statis_unline'] = isset($data['no_statis_unline'])?1:0;
			if (empty($data['exam_id']) && empty($data['subject_id']) && empty($data['collect_fields']) && empty($data['collect_mode'])){
				$this->frameAjaxMessage(null,'数据不能为空');
				exit();
			}else {
				$model = $model->findByPk($data['id']);
				$data['update_user']= $this->cLoginUser['cuser']['id'];
				$data['update_time']= time();
				if ($data['collect_mode']==1){
					if (empty($data['collect_questions'])){
						$this->frameAjaxMessage(null,'题型选择不能为空');
						exit();
					}else {
                        $data['collect_question'] = '';
						$data['collect_questions'] = $data['collect_questions'];
						$model->attributes = $data;
						$model->save();
                        $this->frameAjaxMessage(null,null,true);
                        exit;
					}
				}
				if ($data['collect_mode']==2){
					if (empty($data['collect_question'])){
						$this->frameAjaxMessage(null,'题号选择不能为空');exit();
					}else {
                        $data['collect_questions'] = 0;
						$data['collect_question'] = implode(",",$data['collect_question']);
						$model->attributes = $data;
						$model->save();
						//同步小题分数汇总
						/*$zExam = new zExam();
						$data = $zExam->_getRuleList($data['collect_fields'],$data['exam_id']);*/
						$this->frameAjaxMessage(null,null,true);
						exit;
					}
				}
			}
		}
		$date['data'] = $model->findByPk($id)->attributes;
		$this->render('update',array('date'	=> $date));
	}
	//删除
	public function actionDelete() {
		$id = (int)Yii::app()->request->getparam('id');
		if(!empty($id)){
			$model = QuestionSum::model()->findByPk($id);
			if(empty($model)){
				$this->runShowMessge('/question_collect/default/index',"您要删除的数据不存在!");exit;
			}
			if ($model->delete()){
				$this->redirect('/question_collect/default/index');
			}else {
				$this->runShowMessge('/question_collect/default/index',"删除失败!");
			}
		}else{
			$this->runShowMessge('/question_collect/default/index',"删除失败,参数错误!");
		}
	}
	//获取科目的题型
	public function actionsujectquestions(){
		$subject_id = $this->request->getParam('subject_id');
		$subjecttype = SmallProblem::_getquestions($subject_id);
		echo json_encode($subjecttype);exit;
	}
	//获取科目的题型
	public function actionsujectquestion($id=''){
		$exam_id = $this->request->getParam('exam_id');
		$subject_id = $this->request->getParam('subject_id');
		$where = ' AND "exam_id" ='.$exam_id.' AND "subject_id" ='.$subject_id;
		$subjectquestion = SmallProblem::_getquestion_num($where);
		echo json_encode($subjectquestion);
	}
	//获取项目包含的科目
	function actionAjaxvalue(){
            $this->__common();
		$id = Yii::app()->request->getParam('id');
            $subjectinfos = array();
            $wh = '';
            if ($this->user_role_list != 1) {//科目权限设定
                $wh = ' AND "subject_id" IN (' . $this->user_subject_list . ')';
            }
		//得到科目信息
		$subject_where = ' AND "exam_id"='.$id;
		$subjectinfo = ZTask::_getExamAll($subject_where);
            if (isset($subjectinfo[0]['exam_subject_ids']) && !empty($subjectinfo[0]['exam_subject_ids'])) {
		$subject_code = "'".str_replace("-","','",$subjectinfo[0]['exam_subject_ids'])."'";
                $sujectsql = 'SELECT * FROM "{{subject}}" WHERE "is_del"=0 AND "subject_code" IN (' . $subject_code . ') ' . $wh;
		$subjectinfos = Yii::app()->db->createCommand($sujectsql)->queryAll();
            }
		echo json_encode($subjectinfos);
	}
}